package com.czy.service.impl;

import com.czy.dto.AddBorrowRecordDto;
import com.czy.dto.AdminBorrowPageDto;
import com.czy.dto.UpdateAssetQuantityDto;
import com.czy.dto.UserBorrowPageDto;
import com.czy.entity.BorrowRecord;
import com.czy.exception.SystemException;
import com.czy.mapper.AssetMapper;
import com.czy.mapper.BorrowRecordMapper;
import com.czy.mapper.UserMapper;
import com.czy.service.BorrowRecordService;
import com.czy.utils.BeanCopyUtils;
import com.czy.utils.ThreadLocalUtils;
import com.czy.vo.BorrowRecordPageVo;
import com.czy.vo.PageVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BorrowRecordServiceImpl implements BorrowRecordService {

    @Autowired
    private BorrowRecordMapper borrowRecordMapper;

    @Autowired
    private AssetMapper assetMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 分页查询用户借用资产记录
     */
    @Override
    public PageVo list(UserBorrowPageDto userBorrowPageDto) {

        PageHelper.startPage(userBorrowPageDto.getPage(), userBorrowPageDto.getPageSize());

        userBorrowPageDto.setId(ThreadLocalUtils.getCurrentId());

        List<BorrowRecordPageVo> borrowRecordList = borrowRecordMapper.userPage(userBorrowPageDto);

        Page<BorrowRecordPageVo> page = (Page<BorrowRecordPageVo>) borrowRecordList;

        return new PageVo(page.getTotal(), page.getResult());
    }

    /**
     * 归还资产数量
     */
    @Transactional //关键：声明事务
    @Override
    public void repaid(Integer id) {
        //要补全实际归还时间
        LocalDateTime now = LocalDateTime.now();

        borrowRecordMapper.updateReturnTime(id, now);

        //资产归还后，要根据借出表对应资产id去增加库存数量
        //计算出增加的库存数量
        UpdateAssetQuantityDto updateQuantity = borrowRecordMapper.addAssetNumById(id);

        //增加资产表对应的资产数量
        assetMapper.UpdateStockQuantity(updateQuantity);
    }

    /**
     * 借出资产
     */
    @Transactional //关键：声明事务
    @Override
    public void addBorrowRecord(AddBorrowRecordDto addBorrowRecordDto) {

        BorrowRecord borrowRecord = BeanCopyUtils.copyBean(addBorrowRecordDto, BorrowRecord.class);

        //补全基础属性，当前借用的时间和当前借出用户id
        borrowRecord.setBorrowTime(LocalDateTime.now());
        borrowRecord.setUserId(Long.valueOf(ThreadLocalUtils.getCurrentId()));

        //添加借用资产记录
        borrowRecordMapper.addBorrowRecord(borrowRecord);

        //减少指定资产的库存数量，并返回更新后的库存信息
        UpdateAssetQuantityDto quantityDto = assetMapper.reduceAssetNumById(borrowRecord);

        if (quantityDto.getStockQuantity() < 0){
            throw new SystemException(412,"借用数量错误，请修改后再借");
        }

        //修改资产表的库存量
        assetMapper.UpdateStockQuantity(quantityDto);


    }

    /**
     * 分页查询所有借用资产记录
     */
    @Override
    public PageVo adminList(AdminBorrowPageDto adminBorrowPageDto) {

        //调用PageHelper记录总数和分页操作
        PageHelper.startPage(adminBorrowPageDto.getPageNum(),adminBorrowPageDto.getPageSize());

        //执行SQL语句
        List<BorrowRecordPageVo> borrowRecordList = borrowRecordMapper.adminPage(adminBorrowPageDto);

        //封装结果
        Page<BorrowRecordPageVo> page = (Page<BorrowRecordPageVo>) borrowRecordList;
        return new PageVo(page.getTotal(),page.getResult());
    }
}
